<?php
	if(!$_POST)
		die('Wrong entry point!!!');
		
	@extract($_REQUEST);
	define('JPATH_BASE', dirname(__FILE__) );
	define( 'DS', DIRECTORY_SEPARATOR );
	define( '_JEXEC', 1 );
	require_once(JPATH_BASE.DS."include".DS."arrays.php");
	require_once(JPATH_BASE.DS."include".DS."setup.php");
	require_once(JPATH_BASE.DS."include".DS."functions.php");
	
	$sql = "select * from settings";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	$servicetax_charge = $row['servicetax_charge'];
	$ecess_charge = $row['ecess_charge'];
	$hsecess_charge = $row['hsecess_charge'];
	
	$sql = "SELECT 
		tbl1.companyname `company_name`,
		sum(tbl2.total_charge) `bill_amount`, 
		tbl1.surcharge `fuel_charge`,
		tbl1.service_tax `apply_servicetax`,
		tbl2.document_id `bill_type`
		FROM companies tbl1 INNER JOIN inventories tbl2
		ON tbl1.id = tbl2.company_id
		WHERE company_id IN (".implode(',', $company_ids).") AND booking_date 
		BETWEEN '".db_date_format($period_from)."' AND '".db_date_format($period_to)."'
		GROUP BY document_id, company_id
		ORDER BY company_name, bill_type
		";
	$result = mysql_query($sql);
	while($row = mysql_fetch_array($result)){
		$company_name = $row['company_name'];
		$bill_amount = $row['bill_amount'];
		$fuel_charge = $row['fuel_charge'];
		$bill_type = $slab_type_arr[$row['bill_type']];
		$apply_servicetax = $row['apply_servicetax'];
		
		$fuel_charge = ($fuel_charge / 100 * $bill_amount);
		if($apply_servicetax == 1) {
			$service_tax = ($servicetax_charge / 100 * ($bill_amount + $fuel_charge));
			$ecess_tax = ($ecess_charge / 100 * $service_tax);
			$hsecess_tax = ($hsecess_charge / 100 * $service_tax);
			$service_tax = $service_tax + $ecess_tax + $hsecess_tax;
		} else {
			$service_tax = '0.00';
		}
		$net_amount = $bill_amount + $fuel_charge + $service_tax;
		
		$statement_data_arr[$company_name][$bill_type] = $bill_amount . "[><]" . $fuel_charge . "[><]" . $service_tax . "[><]" . $net_amount;
		
	}
	
	if(!is_array($statement_data_arr)){
		$message = "Their is no Outstanding for this Period.";
	}
	
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<title><?php echo $lang['CompanyNameWithBusiness'] ?></title>
<link rel="stylesheet" href="css/print.css" type="text/css" />
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0" align="center" width="90%">
	<tr>
		<td align=center colspan=2>
			<h3><?php echo $lang['CompanyName'] ?></h3>
			<?php echo $lang['BusinessName'] ?>
			<h5><?php echo $lang['CompanyAddress'] ?></h5>
			<font color=red>From : <?php echo $period_from?> - To : <?php echo $period_to?></font>
		</td>
	</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" align="center" width="90%" class="print-report">
	<tr>
		<td colspan="7"><hr style="border: 1px solid #000"></td>
	</tr>
	<tr>
		<th align=left width=5%>#</th>
		<th align=left>Company Name</th>
		<th align=left>Bill Type</th>
		<th align=right>Bill Amount</th>
		<th align=right>Fuel Charge</th>
		<th align=right>Service Charge</th>
		<th align=right>Net Amount</th>
	</tr>
	<tr>
		<td colspan="7"><hr style="border: 1px solid #000"></td>
	</tr>
	<?php
		if(isset($message)){
			echo '<tr>
			<td align=left colspan="7">'.$message.'</td>
			<tr>';
			exit;
		}	
		$t_bill_amount = 0;
		$t_fuel_charge = 0;
		$t_service_charge = 0;
		$t_net_amount = 0;
		$nm = 1;
		foreach($statement_data_arr as $key => $val) {
			foreach($val as $k => $v) {
				$temp = explode("[><]", $v);
				$t_bill_amount = $t_bill_amount + $temp[0];
				$t_fuel_charge = $t_fuel_charge + $temp[1];
				$t_service_charge = $t_service_charge + $temp[2];
				$t_net_amount = $t_net_amount + $temp[3];
				
	?>
			
			<tr>
			<td align=left><?php echo $nm ?></td>
			<td align=left><?php echo $key?></td>
			<td align=left><?php echo $k?></td>
			<td align=right><?php echo number_format($temp[0], 2)?></td>
			<td align=right><?php echo number_format($temp[1], 2)?></td>
			<td align=right><?php echo number_format($temp[2], 2)?></td>
			<td align=right><?php echo number_format($temp[3], 2)?></td>
			</tr>
	<?php
				$nm++;
			}
			
		}
	?>
	<tr>
		<td colspan="7"><hr style="border: 1px solid #000"></td>
	</tr>
	<tr>
		<td align=right colspan=3>
			Total
		</td>
		<td align=right>
			<?php echo number_format($t_bill_amount, 2)?> 
		</td>
		<td align=right>
			<?php echo number_format($t_fuel_charge, 2)?> 
		</td>
		<td align=right>
			<?php echo number_format($t_service_charge, 2)?> 
		</td>
		<td align=right>
			<?php echo number_format($t_net_amount, 2)?> 
		</td>
	</tr>
	<tr>
		<td colspan="7"><hr style="border: 1px solid #000"></td>
	</tr>
</table>
</body>
</html>